<template>
<div>
   <PanelSplitTwo bottomToolbarHeight="0" :num="2" :sizes="28">
    <template #top>
      <SetUserSelect
        class="queryForm"
        v-model:formData="orderSelect"
        v-model:queryParams="paginationParams"
        @selectFunc="selectFunc"
        :labelwidth="110"
      ></SetUserSelect>
    </template>
     <template #centerToolbar>
        <div class="vex_flex">
          <el-row :gutter="20" style="margin-bottom: 10px">
            <el-col :span="1.5">
              <el-button  :disabled="selectedList.length !=1" @click="handleQuery(1)">查看</el-button>
            </el-col>
             <el-col :span="1.5">
              <el-button :disabled="selectedList.length !=1" @click="cliCAuditFun">同意</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button :disabled="selectedList.length !=1" @click="cliRefuseFun">驳回</el-button>
            </el-col>
            <!-- <el-col :span="1.5">
              <el-button :disabled="selectedList.length !=1" @click="commitFun">同意驳回</el-button>
            </el-col> -->
            <el-col :span="1.5">
              <el-button :disabled="selectedList.length !=1" @click="nopassFun">未通过</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button  @click="handleExport">导出</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button  @click="ImportingFiles">导入审核通过</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button  @click="ImportingFiles(1)">导入驳回</el-button>
            </el-col>
            <!-- <el-col :span="1.5">
              <el-button :disabled="selectedList.length !=1" @click="handleQuery(1)">修改</el-button>
            </el-col> -->
            <!-- <el-col :span="1.5">
              <el-button @click="handleQuery">取消</el-button>
            </el-col> -->
          </el-row>
        <VxeTableGrid
          style="flex: 1;"
          :tableColumn="tableColumn"
          :tableLoading="tableLoading"
          :dataList="dataList"
          :showPagination="true"
          :paginationParams="paginationParams"
          @getList="setQueryParams"
          @selectedChange="selectedChange"
        >
        </VxeTableGrid>
      </div>
      <cliRefuseDom ref="cliRefuseRef" :id="Id" url="/task/visit/refuse" @updateData="updateDataFun"></cliRefuseDom>
     </template>
   </PanelSplitTwo>
    <FileUploadNew :url="urls" ref="fileIstrue" @succFun="succFun"></FileUploadNew>
   </div>
</template>

<script setup lang="jsx">
import { useRouter } from 'vue-router'
import { visitList, delvisit, cliCAudit, cliRefuse, uncommit, nopass  } from "@/api/visitTask";
import { getCurrentInstance, ref } from "vue";
import datas from './data.js'
let keys = ref(0);
const { paginationParams, orderSelect }  = datas
const { proxy } = getCurrentInstance();
const router= useRouter ()
const dialogVisible = ref(false)
const handleClose = () => {
  dialogVisible.value = false
}
const { task_status_all } = proxy.useDict("task_status_all");

const starTimeFun = (time) => {
  paginationParams.value.startTime = time
}
const endTimeFun = (time) => {
  if (paginationParams.value.startTime) {
    if (new Date(paginationParams.value.startTime).getTime()<new Date(time).getTime()) {
      paginationParams.value.endTime = time
    } else {
      paginationParams.value.endTime = time
      proxy.$modal.msgWarning('结束时间不能大于开始时间')
    }
  }
  
}
const dataLists = ref([])
const tableColumn = ref([
  { type: "checkbox", width: 60, fixed: null },
  { field: "name", title: "任务名称" },
  { field: "year", title: "年份" },
  { field: "month", title: "月份" },
  { field: "drugName", title: "品种"},
  { field: "agencyName", title: "代理商"},
  { field: "mrName", title: "代表"},
  { field: "name", title: "拜访名称"},
  { field: "hospital", title: "医院" },
  { field: "departmentName", title: "科室" },
  { field: "doctorName", title: "医生" },
  { field: "target", title: "目的"},
  // { field: "province", title: "推广公司"},
  
  
  { field: "startTime", title: "开始时间",
    slots: { default: ({ row }, h) => {
      return [<div onClick={() => {starTimeFun(row.startTime)}} style="color: rgba(73, 173, 251, 1);cursor: pointer;">{row.startTime}</div>]
    }}
  },
  { field: "endTime", title: "结束时间",
    slots: { default: ({ row }, h) => {
      return [<div onClick={() => {endTimeFun(row.endTime)}} style="color: rgba(73, 173, 251, 1);cursor: pointer;">{row.endTime}</div>]
    }}
  },
  
 
  { field: "status", title: "状态",
  slots: { default: ({ row }, h) => {
    return [<dict-tag options={ task_status_all.value } value={ row.status }></dict-tag>]
  }} },

  { field: "isUnusual", title: "是否异常",
  slots: { default: ({ row }, h) => {
    return row.isUnusual == 1? '是':'否'
  }} },
  { field: "unusualReason", title: "异常原因"},
])
const onlineList = ref([]);
const total = ref(0);
const pageNum = ref(1);
const pageSize = ref(10);
let tableLoading = ref(false)
const dataList = ref([
])

const queryParams = ref({
  ipaddr: undefined,
  userName: undefined
});
// 切换分页
  function setQueryParams(val) {
    paginationParams.value.pageNum = val.pageNum;
    paginationParams.value.pageSize = val.pageSize;
    hospitalLists()
  }
  let Id = ref('')
  // 选择表格
  let selectedList = ref([])
  let status = ref(null)
  function selectedChange(val) {
    Id.value = val.currentItem.id
    status.value = val.currentItem.status
    selectedList.value = val.selectedList
  }
/** 新增|修改 */
function handleQuery(type) {
  router.push({
    path: '/visitTask/addVisitTaska',
    query: {
      id: type == 1? Id.value : undefined,
      type: type == 1? undefined : 'add'
    }
  })
}

const hospitalLists = () => {
  if (paginationParams.value.startTime && paginationParams.value.endTime) {
    if (new Date(paginationParams.value.startTime).getTime()<new Date(paginationParams.value.endTime).getTime()) {
      // paginationParams.value.endTime = time
    } else {
      proxy.$modal.msgWarning('结束时间不能大于开始时间')
      return
    }
  }
  // platformNum
  visitList({
    ...paginationParams.value,
    setTime: undefined
    // startTime: paginationParams.value.setstartTime?.[0]??undefined,
    // endTime: paginationParams.value.setstartTime?.[1]??undefined,
  }).then((res) => {
    paginationParams.value.total = res.total
    dataList.value = res.rows
    selectedList.value= []
  })
}
const updateDataFun = () => {
  hospitalLists()
}
hospitalLists()
const selectFunc = () => {
  paginationParams.value.pageNum = 1
  hospitalLists()
}
const cliCAuditFun = () => {
  if (status.value != 8) {
    proxy.$modal.msgWarning('代理商同意的任务才能同意')
    return
  }
  cliCAudit({
    id: Id.value
  }).then((res) => {
    if (res.status == 0) {
      proxy.$modal.msgSuccess('已同意')
    } else {
      proxy.$modal.msgError(res.msg);
    }
    hospitalLists()
  })
}
const cliRefuseFun = () => {
  if (status.value < 2) {
    proxy.$modal.msgWarning('已提交的任务才能驳回')
    return
  }
  proxy.$refs['cliRefuseRef'].show()
}
const handleExport = () => {
  proxy.download("/task/visit/export", {
    ...paginationParams.value
  }, `config_${new Date().getTime()}.xlsx`);
}

const commitFun = () => {
  if (status.value != 7) {
    proxy.$modal.msgWarning('申请驳回的任务才能同意驳回')
    return
  }
  uncommit({
    id: Id.value
  }).then((res) => {
    proxy.$modal.msgSuccess('已同意驳回')
    hospitalLists()
  })
}

const nopassFun = () => {
  if (status.value != 2) {
    proxy.$modal.msgWarning('已提交的任务才能不通过')
    return
  }
  nopass({
    id: Id.value
  }).then((res) => {
    proxy.$modal.msgSuccess('不通过成功')
    hospitalLists()
  })
}

const succFun = (val) => {
  if (val.status == 0) {
    proxy.$modal.msgSuccess(`上传成功`);
    hospitalLists()
  } else {
    ElMessageBox.confirm(
      val.msg,
      {
        confirmButtonText: '确认',
        type: 'warning',
        draggable: true,
        showCancelButton: false,
      }
    )
  }
}

const urls = ref('/task/visit/import-refuse')
const fileIstrue = ref('fileIstrue')
const ImportingFiles = (type) => {
  if (type == 1) {
    urls.value = '/task/visit/import-refuse'
  } else {
    urls.value = '/task/visit/import'
  }
  console.log('3214312351',  fileIstrue.value)
  fileIstrue.value.openTure()
  // proxy.$refs.fileIstrue.openTure()
  
}
</script>
<style scoped>
  .app-container {
    /* display: flex;
    flex-direction: column;
    height: calc(100vh - 85px);
    overflow: hidden; */
  }
  .vex_flex {
    display: flex;
    flex-direction: column;
    overflow: hidden;
    height: 100%;
  }
  .flexmin {
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }
</style>